<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Azure;
use App;
use Config;
use WindowsAzure\Common\ServiceException;
use Request;
use Response;
use Log;
use Input;
use Redirect;
class BlobsController extends Controller
{

    var $blob;

    public function __construct()
    {
        Log::info(session('access-key'));

        if (session()->has('account') && session()->has('access-key')) {
            $connection = "DefaultEndpointsProtocol=https;AccountName=" . session('account') . ";AccountKey=" . session('access-key');
            Log::info(session('access-key'));
            $this->blob = Azure::getInstance()->createBlobService($connection);
        }
        else {
            session()->flash('status','Please Log in');
            return Redirect::to('/')->send();
        }
    }

    public function index()
    {

    }

    /**
     * This function retrieves the items of a particular blob and return the view with array
     *
     * @param $container Case-sensitive name of the blob
     * @return \Illuminate\View\View    Returns the view
     */
    public function listBlobs($container)
    {
        try {
            // List blobs.
            $blob_list = $this->blob->listBlobs($container);
            $data['blobs'] = $blob_list->getBlobs();
            $data['title'] = "Blobs";
            $data['heading'] = "Blobs - " . $container;
            $data['container'] = $container;
            return view('blobs', $data);
        }
        catch (ServiceException $e) {
            // Handle exception based on error codes and messages.
            // Error codes and messages are here:
            // http://msdn.microsoft.com/library/azure/dd179439.aspx
            $code = $e->getCode();
            $error_message = $e->getMessage();
            echo $code . ": " . $error_message . "<br />";
        }

        return view('welcome');
    }

    /**
     * This function returns the list of Containers. Please note the container needs to call getName method to get
     * the name of the container. Use that name to pass to the listBlobs function to get list of Blobs
     *
     * @return \Illuminate\View\View|string returns the view with list of containers
     */
    public function listContainers()
    {
        $data['title'] = "Containers";
        $data['heading'] = "Containers";
        $data['containers'] = $this->getContainerslist();
        return view('containers', $data);
    }

    public function getContainerslist()
    {
        try {
            $containers_list = $this->blob->listContainers();
            $containers = $containers_list->getContainers();
            return $containers;
        }
        catch (ServiceException $e) {
            $code = $e->getCode();
            $error_message = $e->getMessage();
            return $code . ": " . $error_message . "<br />";
        }
    }

    public function countContainers()
    {
        return count($this->getContainerslist());
    }

    public function createContainer()
    {
        $data = Request::all();
        try {
            // Create container.
            $this->blob->createContainer($data['container']);
            $data['statusCode'] = 200;
            return json_encode($data);
        }
        catch (ServiceException $e) {
            // Handle exception based on error codes and messages.
            // Error codes and messages are here:
            // http://msdn.microsoft.com/library/azure/dd179439.aspx
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
    }

    public function deleteContainer()
    {
        $data = Request::all();
        try {
            // Create container.
            $this->blob->deleteContainer($data['container']);
            $data['statusCode'] = 200;
            return json_encode($data);
        }
        catch (ServiceException $e) {
            // Handle exception based on error codes and messages.
            // Error codes and messages are here:
            // http://msdn.microsoft.com/library/azure/dd179439.aspx
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
    }


    /*
     *  This function uploads the blob on Azure Storage Blob
     */
    public function uploadBlob()
    {
        $data = Request::all();
        try {
            $file = Request::file('blob');
            $file->move('temp/',$file->getClientOriginalName());

            // Create container.
            $this->blob->createBlockBlob($data['container'], $data['name'], $data['blob']);
            $data['statusCode'] = 200;
            return json_encode($data);
        }
        catch (ServiceException $e) {
            // Handle exception based on error codes and messages.
            // Error codes and messages are here:
            // http://msdn.microsoft.com/library/azure/dd179439.aspx
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
    }


    public function deleteBlob()
    {
        $data = Request::all();
        try {
            // Create container.
            $this->blob->deleteBlob($data['container'], $data['blob']);
            $data['statusCode'] = 200;
            return json_encode($data);
        }
        catch (ServiceException $e) {
            // Handle exception based on error codes and messages.
            // Error codes and messages are here:
            // http://msdn.microsoft.com/library/azure/dd179439.aspx
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
    }

    public function getBlobDetails()
    {
        $lastModified = $blob->getProperties()->getLastModified()->format('Y-m-d H:i:s');
        $contentType = $blob->getProperties()->getContentType();
        $contentEncoding = $blob->getProperties()->$blob->getProperties()->getContentEncoding();
        $blobType = $blob->getProperties()->getBlobType();
        $Url = $blob->getURL();

    }
}
